package pro.shuangxi.config.spring;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import pro.shuangxi.model.book.helper.book.BookRepository;

import javax.sql.DataSource;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Configuration
@ComponentScan(value = "pro", excludeFilters = {@Filter(type = FilterType.ANNOTATION, classes = {Controller.class}),@Filter(type = FilterType.REGEX,pattern = "pro.shuangxi.config.*Config")})
@Import(value = {PropertiesConfig.class})
public class RootConfig {
	@Autowired
	Environment environment;

	@Bean
	public DataSource initDataSource() {
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setUsername(environment.getProperty("jdbc.username"));
		dataSource.setPassword(environment.getProperty("jdbc.password"));
		dataSource.setDriverClassName(environment.getProperty("jdbc.driver"));
		dataSource.setUrl(environment.getProperty("jdbc.url"));
		return dataSource;
	}
	@Bean("books")
	public BookRepository initBookRepository(){
		return new BookRepository();
	}
	@Bean("threadPool")
	public ExecutorService initThreadPool(){
		return Executors.newFixedThreadPool(10);
	}

}
